TEXTURE2D (_SpecGlossMap);
SAMPLER (sampler_SpecGlossMap);
TEXTURE2D (_OcclusionMap);
SAMPLER (sampler_OcclusionMap);

half SampleOcclusion(float2 uv)
{
    #ifdef _OCCLUSIONMAP
    // TODO: Controls things like these by exposing SHADER_QUALITY levels (low, medium, high)
    #if defined(SHADER_API_GLES)
    return SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;
    #else
    half occ = SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;
    return LerpWhiteTo(occ, _OcclusionStrength);
    #endif
    #else
    return half(1.0);
    #endif
}


//unity的源码实现的
half4 SampleSpecularSmoothness(float2 uv, half alpha, half4 specColor, TEXTURE2D_PARAM (specMap, sampler_specMap))
{
    half4 specularSmoothness = half4(0, 0, 0, 1);
    #ifdef _SPECGLOSSMAP
    specularSmoothness = SAMPLE_TEXTURE2D(specMap, sampler_specMap, uv) * specColor;
    #elif defined(_SPECULAR_COLOR)
    specularSmoothness = specColor;
    #endif

    #ifdef _GLOSSINESS_FROM_BASE_ALPHA
    specularSmoothness.a = alpha;
    #endif

    return specularSmoothness;
}

/*
struct SurfaceData
{
    half3 albedo;
    half3 specular;
    half  metallic;
    half  smoothness;
    half3 normalTS;
    half3 emission;
    half  occlusion;
    half  alpha;
    half  clearCoatMask;
    half  clearCoatSmoothness;
};
             */
void InitSurfaceData(Varyings IN, out SurfaceData surfaceData)
{
    surfaceData = (SurfaceData)0;
    half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
    #ifdef _ALPHATEST_ON
        clip(baseMap.a - _Cutoff);
    #endif
    half4 diffuse = baseMap * _BaseColor * IN.color;

    surfaceData.albedo = diffuse.rgb;
    surfaceData.normalTS = SampleNormal(IN.uv, _BumpMap, sampler_BumpMap);
    surfaceData.emission = SampleEmission(IN.uv, _EmissionColor, _EmissionMap, sampler_EmissionMap);
    //surfaceData.occlusion = 1.0;
    //
    surfaceData.occlusion = SampleOcclusion(IN.uv);
    half4 specular = SampleSpecularSmoothness(IN.uv, baseMap.a, _SpecColor, _SpecGlossMap, sampler_SpecGlossMap);
    surfaceData.specular = specular.rgb;
    surfaceData.smoothness = specular.a * _Smoothness;
}
